Mocking Service 2 API
Getting Started
This page is an overview of the Mocking Service 2 API.
Mocking Service 2 provides the following operation to manage API mocks:
- Create or delete mock instances for a given API in Exchange, VCS or API Manager.
- Create, get or delete shareable links of mock instances
- Access to mocked resources
1. Getting Credentials
Almost every operation in the Mocking Service 2 API needs authorized access meaning that an authentication token is required to be sent as part of the request.
To get this token, we need to perform a POST request sending the username and password to https://anypoint.mulesoft.com/accounts/login
Example:
$ curl -X POST \
https://anypoint.mulesoft.com/accounts/login \
-H 'Content-Type: application/json' \
-d '{"username": "anypoint","password": "mulesoft"}'
{"access_token": "071e65a7-35c7-465a-8e33-ea68659099bf","token_type": "bearer","redirectUrl": "/home/"}
2. Accessing mocked resources
Unlike former Mocking Service, there is no need to explicitly create a mock instance before accessing a resource. Mock instances will be automatically created the first time a request is performed.
Depending on the source of the API, a specific endpoint is provided to access the mocked resources:
- VCS:
https://anypoint.mulesoft.com/mocking/api/v1/sources/vcs/projects/{projectId}/{branch}/m/{resource}
- Exchange:
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/{groupId}/{assetId}/{version}/m/{resource}
- API Manager:
https://anypoint.mulesoft.com/mocking/api/v1/sources/manager/apis/{orgId}/{apiId}/{versionId}/m/{resource}
By Default, Mocking Service 2 works statically according to the API specification, please refer to Behavioral Headers to know how to customize the responses
Example:
Suppose we have the following asset in Exchange,
groupId: cb0e4f00-5026-4999-b5a5-dc2307b886c5
assetId: api-example
version: 1.0.0
that contains the following API definition,
#%RAML 1.0
title: API Example
version: v1
/ping:
get:
responses:
200:
body:
application/json:
example:
status: OK
we can perform a GET request to /ping
as follow:
$ curl -X GET \
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/cb0e4f00-5026-4999-b5a5-dc2307b886c5/api-example/1.0.0/m/ping \
-H 'MS2-Authorization: Bearer 071e65a7-35c7-465a-8e33-ea68659099bf'
{"status":"OK"}
3. Getting the mock instance metadata
Commonly, a base URI is defined as part of the API. This information will be part of the mock metadata and it can be requested to each mock. There is a specific endpoint to get the mock metadata, depending on the source of the API:
- VCS:
https://anypoint.mulesoft.com/mocking/api/v1/sources/vcs/projects/{projectId}/{branch}
- Exchange:
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/{groupId}/{assetId}/{version}
- API Manager:
https://anypoint.mulesoft.com/mocking/api/v1/sources/manager/apis/{orgId}/{apiId}/{versionId}
Example:
$ curl -X GET \
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/cb0e4f00-5026-4999-b5a5-dc2307b886c5/api-example/1.0.0 \
-H 'MS2-Authorization: Bearer 071e65a7-35c7-465a-8e33-ea68659099bf'
{"id":"cb0e4f00-5026-4999-b5a5-dc2307b886c5-api-example-1.0.1","metadata":{"baseUriPath":"/"}}
4. Shareable Links
Shareable links are the way to access mocked resources without being authenticated.
4.1. Managing Shareable Links
Depending on the source of the API, there is a specific endpoint to create, get or delete a link to a mock instance:
- VCS:
https://anypoint.mulesoft.com/mocking/api/v1/sources/vcs/projects/{projectId}/{branch}/link
- Exchange:
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/{groupId}/{assetId}/{version}/link
- API Manager:
https://anypoint.mulesoft.com/mocking/api/v1/sources/manager/apis/{orgId}/{apiId}/{versionId}/link
Example:
Create a new shareable link, if not exists.
$ curl -X POST \
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/cb0e4f00-5026-4999-b5a5-dc2307b886c5/api-example/1.0.0/link \
-H 'MS2-Authorization: Bearer 071e65a7-35c7-465a-8e33-ea68659099bf'
{"id": "707c4cb0-8a8f-4b32-86a2-16c75a1323e3"}
Return the shareable link metadata:
$ curl -X GET \
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/cb0e4f00-5026-4999-b5a5-dc2307b886c5/api-example/1.0.0/link \
-H 'MS2-Authorization: Bearer 071e65a7-35c7-465a-8e33-ea68659099bf'
{"id": "707c4cb0-8a8f-4b32-86a2-16c75a1323e3"}
Remove a sharable link:
$ curl -i -X DELETE \
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/cb0e4f00-5026-4999-b5a5-dc2307b886c5/api-example/1.0.0/link \
-H 'MS2-Authorization: Bearer 071e65a7-35c7-465a-8e33-ea68659099bf'
HTTP/1.1 204 No Content
Date: Fri, 31 May 2019 16:26:54 GMT
Server: nginx
Strict-Transport-Security: max-age=60; includeSubDomains
Connection: keep-alive
4.2. Accessing Mock Instances using Shareable Links
Once a shareable link is created, mocked resources can be requested at the following unauthenticated endpoint: /links/{id}/{resource}
Example:
$ curl https://anypoint.mulesoft.com/mocking/api/v1/links/707c4cb0-8a8f-4b32-86a2-16c75a1323e3/ping
{"status":"OK"}
5. Mock Eviction
As mock instances are generated on-demand and then cached, there are some situations where the source of the mock could have mutated and the mock could have become inconsistent in case it is cached. For those cases, there is an endpoint that forces the mock eviction so it must be rebuilt upon the next request.
In other words, to evict a mock instance, we need to perform a DELETE request to one of the following endpoints, according to the source of the API:
- VCS:
https://anypoint.mulesoft.com/mocking/api/v1/sources/vcs/projects/{projectId}/{branch}
- Exchange:
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/{groupId}/{assetId}/{version}
- API Manager:
https://anypoint.mulesoft.com/mocking/api/v1/sources/manager/apis/{orgId}/{apiId}/{versionId}
Example:
$ curl -i -X DELETE \
https://anypoint.mulesoft.com/mocking/api/v1/sources/exchange/assets/cb0e4f00-5026-4999-b5a5-dc2307b886c5/api-example/1.0.0 \
-H 'MS2-Authorization: Bearer 071e65a7-35c7-465a-8e33-ea68659099bf'
HTTP/1.1 204 No Content
Date: Fri, 31 May 2019 16:26:54 GMT
Server: nginx
Strict-Transport-Security: max-age=60; includeSubDomains
Connection: keep-alive